<!---Publicity- jquery animation----->
function slideSwitch() {
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});
//$(function() {
// $( document ).tooltip();
//});
//$(document).ready(function(){
  //  var $cs = $('.styled').customSelect();
//});


//Menu
/*Menu click - Resize altura de LI **/
$(document).ready(function() {
    var setColumsHeight = function(cols, height){
        $.each(cols, function(i, val){
            $(val).css("height", height);
        });
    };
    var getMaxColumnsHeight = function(cols) {
        var currentHeight = 0, maxHeight = 0;
        $.each(cols, function(i, val){
            currentHeight = $(val).height();
            if(currentHeight > maxHeight)
            {
                maxHeight = currentHeight;
            }
        });
        return maxHeight;
    }
    //
    var fitMenuColSizeCustom = function(menuRows) {
        $.each(menuRows, function(j, row){
            var cols = $(row).children();
            setColumsHeight(cols, getMaxColumnsHeight(cols));
        });
    }
    var toogleMenuVisibility = function(currentlink){
        $(".main-menu li .hover-menu").slideUp();
        if( $(currentlink).hasClass('hover')) {
            $(".main-menu li").removeClass("hover");
            $(currentlink).removeClass("hover");
            $(currentlink).find(".hover-menu").slideUp();
        } else {
            $(".main-menu li").removeClass("hover");
            $(currentlink).addClass("hover");
            $(currentlink).find(".hover-menu").slideDown();
            fitMenuColSizeCustom($($(currentlink).find(".cont-menu-compuesto > ul")));
        }
    }
    $(".main-menu li").keypress(function(ev){
        if (ev.which === 13){
            toogleMenuVisibility(this);
//            e.preventDefault();
        }
    });
    $(".main-menu li").click(function(e) {
        e.stopPropagation(); //stops click event from reaching document
        toogleMenuVisibility(this);
//        e.preventDefault();
    });
    $(document).click(function() {
        $(".main-menu li").removeClass("hover");
        $(".main-menu li .hover-menu").slideUp();
    });
    try
    {
        document.getElementById("ExternalLink").contentWindow.document.body.onclick = function() {
            $(".main-menu li").removeClass("hover");
            $(".main-menu li .hover-menu").slideUp();
        };
    } catch(e) {}
});

//PlaceHolders
$(document).ready(function() {
    $(".cont-place").click(function() {
        $(".place").removeClass("focus");
        $(".place").focusout();

        var currentPlace =  this;
        $(currentPlace).find($(".place")).focus();
        $(currentPlace).find($(".place")).addClass("focus");
    })
    $(".cont-place").keyup(function() {
        $(".place").removeClass("focus");
        $(".place").focusout();

        var currentPlace =  this;
        $(currentPlace).find($(".place")).focus();
        $(currentPlace).find($(".place")).addClass("focus");

        if($(currentPlace).find($(".place")).val().length > 0 ){
            $(currentPlace).find($(".text-holder")).css("display","none");
        }else{
            $(currentPlace).find($(".text-holder")).css("display","block");
        }
    })
    $(".place").focusout(function(){
        $(this).removeClass("focus");
    });
});

//AGREGA CLASES A LOS TD DE UNA COLUMNA PARA ALINEARLOS
function UpdateTableAligns() {
    //Altera estilos de tablas

    var existingClasses = ["col-center", "col-right", "col-left"], rows = $('table tr:gt(0)');

    var updateRowsByClass = function(className) {
        $("table").each(function() {
            var currentTable = this;
            $(currentTable).find("th." + className).each(function() {
                $(currentTable).find("tr").find('td:nth-child(' + ($(this).index()+1) + ')').addClass(className);
            });
        });
    }

    for(var i = 0; i < existingClasses.length; i++){
        updateRowsByClass(existingClasses[i]);
    }

}

//AGREGA WIDTH A LOS <TH> PARA QUE SEAN DEL MISMO WIDTH QUE LOS TD - SCROLL
/*$(document).ready(function() {

 var fixDividedTableWidth = function() {
 $(".cont-table").each(function() {
 var currentTable = this;

 var contBody = $(currentTable).find(".t-head thead tr")
 var body = $(currentTable).find(".t-body thead th")
 var head = $(currentTable).find(".t-head th")

 var bodyWidth = $($(currentTable).find(".t-body")).width();
 var tHead = $(currentTable).find(".t-head");

 var borderHead =  $(currentTable).width() - bodyWidth;

 $(tHead).css("border-right-width", borderHead);

 $.each(body, function( index, value ) {
 var bodyWidth = $(value).width();
 $( body ).clone().appendTo( contBody );
 $(head[index]).css("width", bodyWidth );
 });

 });
 }
 fixDividedTableWidth();
 });
  */
//AGREGA padding A LOS LI DEL MENÚ PRINCIPAL PARA EQUILIBAR EL MENÚ.
$(document).ready(function() {
    var resetWidthLiMenu = function(){
        var fixPadding = function(){

            var htmlLi = $("ul.main-menu").children();
            var htmlA = htmlLi.find("a");
            var acumWidth = 0;

            $.each(htmlLi, function(i, value){
                acumWidth += $(value).width();
            });
            var doublePaddingWidth = Math.floor((999 - acumWidth) / htmlLi.length);
            var paddingWidth = Math.floor(doublePaddingWidth / 2);

            $.each(htmlA, function(i, value){
                $(value).css("padding-left",paddingWidth);
                $(value).css("padding-right",paddingWidth);
            });
        };
        var resetPadding = function(){
            var updateUl = $("ul.main-menu").outerWidth();
            var restWidth = 999 - updateUl;
            $(".last-child").css("padding-left",restWidth);
        };
        var resetWidthUl = function(){
            $("ul.main-menu").css("display", "block");
            $("ul.main-menu").css("width", "1000px");
        }
        fixPadding();
        resetPadding();
        resetWidthUl();
    }
    resetWidthLiMenu();
});
//AGREGA LA CLASE fila-link-arrow AL ÚLTIMO TD DE UN TR - SÓLO IE 7 y 8
$(function() {
    var rows = $('table.fila-link tr');

    if ($.browser.msie && parseInt($.browser.version) < 9)
    {
        rows.hover( //not(':first')??
            function () {
                $(this).find("td:last").css("background-color","");
                $(this).find("td:last").addClass("fila-link-arrow");
            },
            function () {
                $(this).find("td:last").removeClass("fila-link-arrow");
                $(this).find("td:last").css("background-color","white");

            }
        );
    }
});
$(document).ready(function() {
    $(".fila-link tr").find(" td:last").css("padding-right","20px");

        $( ".btn.dropdown" ).click(function() {
            if($(this).find("ul").css('display') == 'block'){
                $(this).find("ul").slideUp();
            }else{
                $(this).find("ul").slideDown();
            }
        });
   /* $(document).click(function() {
        if($(".btn.dropdown ul").css('display') == 'block'){
         $(".btn.dropdown ul").slideUp();
        }else{
        }
    }); */
 //   $(".cont-search input").focus(function() {
     //   $(".cont-results").css("display","block");
   // });

    var focusContResultLi = function(index)
    {
        var jqElement = $($(".cont-results ul li a")[index]);

        $(jqElement).focus();

        if(jqElement.is(":focus")) {
            $($(".cont-results ul li")[index]).addClass("foco");
        }

    }

    $(".cont-search input").keydown(function(e) {
        if(e.keyCode == 40) {
            $(".cont-search input").blur();
            focusContResultLi(0);

            event.preventDefault(); //prevent scrolling
        }
    });
    $(".cont-search").click(function(ev) {
        $.each($(".cont-results ul li"), function(i, e){
            $(e).removeClass("foco");
        });
    });

    $(".cont-results ul li a").keydown(function(e)
    {
        var jqCurrentElement = $(this);     
        var jqAllLi = $(".cont-results ul li");

        var index = jqAllLi.index(jqCurrentElement.parent());

        var defaultExit = -50; //default exit
        var target;

        switch(e.keyCode)
        {
            case 40: //downKey
                target = index != jqAllLi.length - 1 ? index + 1 : 0;
                break;
            case 38: //upKey
                target = index - 1;
                break;
            default: //???
                target = defaultExit;
                break;
        }

        if(target != defaultExit){
            jqCurrentElement.blur();
            $(jqCurrentElement.parent()).removeClass("foco");
            
            if(target >= 0){
                focusContResultLi(target);
            } else {
                $(".cont-search input").focus();
            }
            
            event.preventDefault(); //prevent scrolling
        }
    });
});